//
//  BusiNetworkManager.swift
//  SwiftNetwork
//
//  Created by 万孟超 on 2022/1/12.
//

import UIKit

class BusiNetworkManager {
    
    @available(*, unavailable, renamed: "该类不支持初始化，请使用类方法")
    init() { }
    
    
    @discardableResult
    static func queryData(target : NSObject? = nil,
                          request : NetworkBaseRequest,
                          successBlock : NetworkCompleteBlock? = nil,
                          errorBlock : NetworkErrorBlock? = nil) -> NetworkEngine {
        let engine = NetworkEngine.init(withBindMethod: request.method)
        engine.delegate = target as? NetowrkProgressProtocol
        target?.networkAutoCancel.addEngine(engine: engine)
        NetworkManager.POST(withRequest: request, wihEnginePoint: engine.enginePoint) { networkProgress in
            engine.delegate?.network(request: request, progress: networkProgress)
        } withCompleteBlock: { result in
            target?.networkAutoCancel.removeEngine(engine: engine)
            if successBlock != nil {
                successBlock!(result)
            }
        } withErrorBlock: { error in
            target?.networkAutoCancel.removeEngine(engine: engine)
            if errorBlock != nil {
                errorBlock!(error)
            }
        }
        return engine
    }

}
